package com.zhijian.wharf.service.impl;

import com.zhijian.wharf.entity.po.ThirdPartyDocking;
import com.zhijian.wharf.entity.vo.request.ThirdPartyDockingSearchVo;
import com.zhijian.wharf.entity.vo.response.ThirdPartyDockingVo;
import com.zhijian.wharf.service.ServerRegisterService;
import com.zhijian.wharf.service.ThirdPartyDockingService;
import com.zhijian.wharf.util.StrUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author HCG
 * @version 1.0
 * @date 2024-08-07
 */
@Slf4j
@Service
public class ServerRegisterServiceImpl implements ServerRegisterService {

    @Autowired
    private ThirdPartyDockingService thirdPartyDockingService;

    @Override
    public boolean register(Long userId, List<Long> moduleList) {
        //查出这个用户对应的第三方对接数据
        ThirdPartyDockingVo thirdPartyDockingVo = thirdPartyDockingService.selectOne(ThirdPartyDockingSearchVo.builder()
                .userId(userId)
                .build());
        if (Objects.nonNull(thirdPartyDockingVo)) {
            String moduleIds = StrUtil.join(",", moduleList);
            return thirdPartyDockingService.updateByPrimaryKeySelective(ThirdPartyDocking.builder()
                    .id(thirdPartyDockingVo.getId())
                    .modules(moduleIds)
                    .build());
        }
        return false;
    }

    @Override
    public List<Integer> selectModules(Long userId) {
        ThirdPartyDockingVo thirdPartyDockingVo = thirdPartyDockingService.selectOne(ThirdPartyDockingSearchVo.builder()
                .userId(userId)
                .build());
        if (Objects.nonNull(thirdPartyDockingVo) && StringUtils.isNotBlank(thirdPartyDockingVo.getModules())) {
            return Arrays.stream(thirdPartyDockingVo.getModules().split(",")).map(Integer::valueOf).collect(Collectors.toList());
        }
        return Collections.emptyList();
    }
}
